package sunwin.yoms.service.system;

import com.github.pagehelper.PageHelper;
import jodd.datetime.JDateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sunwin.yoms.comm.YomsException;
import sunwin.yoms.comm.YomsPage;
import sunwin.yoms.dao.ParkCooperateDao;
import sunwin.yoms.domain.park.ParkCooperateDomain;

import java.util.Date;
import java.util.List;

/**
 * Created by watson on 2015/8/20.
 */
@Service
public class ParkCooperateService {
    @Autowired
    private ParkCooperateDao parkCooperateDao;

    /**
     * 查询
     *
     * @return
     * @throws YomsException
     */
    public List<ParkCooperateDomain> findpark(ParkCooperateDomain parkCooperateDomain, Date startTime, Date endTime) throws YomsException {
        List<ParkCooperateDomain> parkCooperateDomains = parkCooperateDao.getDomainCollectionExtend(parkCooperateDomain, startTime, endTime);
        if (parkCooperateDomains.size() == 0) {
            throw new YomsException("没有找到数据", 1);
        }
        return parkCooperateDomains;
    }

    /**
     * 获取公园合作按时间排序
     */
    public List<ParkCooperateDomain> getParkByTimes(YomsPage yomsPage) throws YomsException {
        ParkCooperateDomain domain = new ParkCooperateDomain();
        PageHelper.startPage(yomsPage.getPageIndex(),yomsPage.getPageSize(),true);
        PageHelper.orderBy("park_create_time desc");
        List<ParkCooperateDomain> domains = parkCooperateDao.getDomainCollection(domain);
        if (domains.size() <= 0) {
            throw new YomsException("没有找到公园合作数据", 1);
        }
        return domains;


    }

    /**
     * id查询单个公园合作
     */
    public ParkCooperateDomain getParkById(int parkId) throws YomsException {
        ParkCooperateDomain parkCooperateDomain = parkCooperateDao.getDomainById(parkId);
        if (parkCooperateDomain == null) {
            throw new YomsException("该数据不存在", 1);
        }
        return parkCooperateDomain;
    }

    /**
     * 总数量
     */
    public int getParkCount() {
        return parkCooperateDao.getDomainCount();
    }

    /**
     * 增加
     */
    public void createPark(ParkCooperateDomain parkCooperateDomain) throws YomsException {
        parkCooperateDomain.setParkCreateTime(new JDateTime(System.currentTimeMillis()).convertToDate());
//        parkCooperateDomain.setParkStartTime(new JDateTime(system.currentTimeMillis()).convertToDate());
//        parkCooperateDomain.setParkEndTime(new JDateTime(system.currentTimeMillis()).convertToDate());

        ParkCooperateDomain domain = new ParkCooperateDomain();
        domain.setParkName(parkCooperateDomain.getParkName());
        List<ParkCooperateDomain> parkCooperateDomains = parkCooperateDao.getDomainByCondition(domain);
        if (parkCooperateDomains.size() > 0) {
            throw new YomsException("该公园名称已存在", 1);
        } else {
            if (parkCooperateDao.addDomain(parkCooperateDomain) != 1) {
                throw new YomsException("增加公园失败");
            }
        }
    }

    /**
     * 删除
     */
    public void deletePark(int id) throws YomsException {

        ParkCooperateDomain domain = new ParkCooperateDomain();
        domain.setParkId(id);
        ParkCooperateDomain parkCooperateDomain = parkCooperateDao.getDomainById(domain.getParkId());
        if (parkCooperateDomain == null) {
            throw new YomsException("非法请求");
        } else {
            if (parkCooperateDao.deleteDomain(parkCooperateDomain) != 1) {
                throw new YomsException("删除公园失败");
            }
        }
    }

    /**
     * 更新
     */
    public void upPark(ParkCooperateDomain parkCooperateDomain) throws YomsException {
        ParkCooperateDomain domain = parkCooperateDao.getDomainById(parkCooperateDomain.getParkId());
        if (domain == null) {
            throw new YomsException("非法请求");
        } else {
            if (parkCooperateDao.updateDomain(parkCooperateDomain) != 1) {
                throw new YomsException("更新公园失败");
            }
        }
    }
}
